// @flow
import React, { Component } from 'react'
import { AgGridReact } from 'ag-grid-react'
import styles from './BomGrid.css'
// import moment from 'moment'
import { TableResize } from 'ag-grid-addons'
// import rowData from './rowData'

const columnDefs = [
  { headerName: '设备编号',
    field: 'deviceNo',
    width: 180,
    minWidth: 180,
    cellStyle: {
      'padding': '8px'
    }
  },
  { headerName: '设备名称',
    field: 'name',
    width: 180,
    cellStyle: {
      'padding': '8px',
      'text-align':'center'
    }
  },
  { headerName: '设备型号',
    field: 'spec',
    // width: 180,
    cellStyle: {
      'padding': '8px',
      'text-align':'center'
    }
  },
  {
    headerName: '状态',
    field: 'status',
    width: 100,
    cellStyle: {
      'padding': '8px',
      'text-align': 'center'
    }
  },
  { headerName: '负责人',
    field: 'owner',
    width: 100,
    cellStyle: {
      'padding': '8px',
      'text-align':'center'
    }
  }
  // { headerName: '生产日期',
  //   field: 'produceDate',
  //   // width: 150,
  //   suppressMenu: true,
  //   cellRenderer: (params) => {
  //     return moment(params.data.produceDate).format('YYYY-MM-DD')
  //   },
  //   cellStyle: {
  //     'padding': '8px',
  //     'text-align':'center'
  //   }
  // }
]

type Props = {
  rowData: Array<Object>
}

export class BomGrid extends Component {
  props: Props
  api: Object
  gridOptions: Object
  onGridReady: () => void
  TableResize: () => void
  constructor (props: Props) {
    super(props)
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      suppressPaginationPanel: true,
      enableServerSideSorting: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }

  render () {
    const { rowData } = this.props
    return (
      <div className={styles['grid']}>
        <div className={styles['content'] + ' ag-fresh'}>
          <AgGridReact
            gridOptions={this.gridOptions}
            rowHeight='34'
            headerHeight='28'
            columnDefs={columnDefs}
            rowData={rowData}
            onGridReady={this.onGridReady}
          />
        </div>
      </div>
    )
  }
}

export default BomGrid
